import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

/* Layout */
import Layout from '@/layout';

// 创建一个导入上下文
const requireContext = require.context('./modules', false, /\.js$/);

// 异步路由数组
export const asyncRoutes = requireContext
  .keys() // 导入上下文 中 所有 匹配的 文件路径 组成的 数组
  .map(requireContext) // 导入每个模块中所有导出的内容，并映射成一个数组
  .map(v => v.default); // 提取每个模块中默认导出的内容，并映射成一个数组

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },

  {
    name: '404',
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true,
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard', key: 'route.dashboard' },
        beforeEnter: (to, from, next) => {
          console.log('beforeEnter');
          next();
        },
      },
    ],
  },

  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'Import',
        component: () => import('@/views/import/index'),
      },
    ],
  },

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true },
];

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: [...constantRoutes],
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();

  // router = newRouter

  // 路由器实例变了吗？   没变
  // 什么变了？  实例内的东东变了， matcher 变了
  // 需要搞清楚 matcher 是什么东东？
  //   在vue-router中，每一个我们定义的路由都会被解析成一个对应的matcher（RouteRecordMatcher类型），路由的增删改查都会依靠matcher来实现。
  // 为什么要这样做呢？
  //  router = newRouter 这样不行吗？   不行，为啥？
  //     因为  在  组件身上已经注入了 this.$router，
  //     router = newRouter  这样赋值，组件实例上的 this.$router 会变吗？ 不会
  //     只有 router.matcher = newRouter.matcher 这样赋值，组件实例上的 $router 才能更新
  router.matcher = newRouter.matcher; // reset router
}

export default router;
